package classes;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class AccountManager {

	static Statement statement; // gasarkvevia staticuri ikos tu ar update
								// metodi
	private static final String table = "users";
	private static final String subjectLecturers = "subject_vs_lecturer";
	private static final String group_vs_lecture_Table = "group_vs_lecturer";
	public static String ACCOUNTMANAGER = "accountManager";


	@SuppressWarnings("static-access")
	public AccountManager(Statement statement) {
		this.statement = statement;
	}

	/**
	 * Creates new account if there is not same UserName in database.
	 * 
	 * @param username
	 * @param name
	 * @param surname
	 * @param status
	 *            Admin, Lector, Strudent, SectionLeader or some of them.
	 * @param info
	 * @param pic
	 * @return true if account is created, false otherwise.
	 */
	public boolean createAccount(String username, String name, String surname,
			String status, String info, String pic) {
		try {
			String generatedPassword = PasswordManager.getRandomPassword();
			String hashedPassword = PasswordManager
					.generatePassword(generatedPassword);
			statement.executeUpdate("Insert into " + table + " values" + "("
					+ "null" + "," + " \'" + username + "\'," + "\'" + name
					+ "\'," + "\'" + surname + "\'," + "\'" + hashedPassword
					+ "\'," + "\'" + status + "\'," + "\'" + info + "\',"
					+ "\'" + "" + "\'," + "\'" + "" + "\'," + "\'" + pic
					+ "\');");
			SendMail mai = new SendMail();
			SendMail.send(username, "HomeWork Manager Password","You are registered to HomeWork Manager. Your Password is " + generatedPassword);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * Checks if there is account registered in database with this username
	 * 
	 * @param username
	 * @return true is username is in database, false otherwise.
	 */
	public boolean isValidUserName(String username) {
		try {
			ResultSet set = statement.executeQuery("Select name from " + table
					+ " where username='" + username + "';");

			set.beforeFirst();
			return set.next();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * Checks if password is correct for the given username.
	 * 
	 * @param username
	 * @param password
	 * @return true if is correct, false otherwise.
	 */
	public boolean isCorrectPassword(String username, String password) {
		if (isValidUserName(username)) {
			try {
				ResultSet set = statement.executeQuery("Select password from "
						+ table + " where username='" + username + "';");
				set.next();
				String gotPassword = set.getString("password");
				return gotPassword.equals(PasswordManager
						.generatePassword(password));

			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}
		}
		return false;
	}

	/**
	 * Deletes Account of given username.
	 * 
	 * @param username
	 * @return true if account exists and is deleted, false otherwise;
	 */
	public boolean deleteAccount(String username) {
		try {
			statement.executeUpdate("delete from " + table
					+ " where username='" + username + "';");
//			statement.executeUpdate("delete from " + group_vs_lecture_Table
//					+ " where student_id=" + username + ";");
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public void dropAllTables() {
		try {
			statement.executeUpdate("truncate table users;");
			statement.executeUpdate("truncate table subjects;");
			statement.executeUpdate("truncate table groups");
			statement.executeUpdate("truncate table group_vs_student");
			statement.executeUpdate("truncate table subject_vs_lecturer;");
			statement.executeUpdate("truncate table homeworkforlecturers;");
			statement.executeUpdate("truncate table homeworkforstudents;");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public User getUser(String username) {
		try {
			ResultSet usr = statement.executeQuery("Select * from " + table
					+ " where username='" + username + "';");
			if (!usr.next()) {
				return null;
			}
			int userId = usr.getInt("user_id");
			String name = usr.getString("name");
			String surname = usr.getString("surname");
			String password = usr.getString("password");
			String status = usr.getString("status");
			String info = usr.getString("info");
			String question = usr.getString("question");
			String answer = usr.getString("answer");
			String pic = usr.getString("pic");
			if (status.equals("3")) {
				ResultSet group = statement
						.executeQuery("Select group_id from group_vs_student where student_id ="
								+ userId + ";");
				ArrayList<Group> subjects = new ArrayList<Group>();
				while (group.next()) {
					subjects.add(GroupManager.getGroup(group.getInt("group_id")));
				}
				User user = new User(userId, username, name, surname, password,
						status, info, question, answer, pic, null, subjects,
						null);
				return user;
			} else if (status.equals("1")) {
				ResultSet rs = statement.executeQuery("Select subject_id from "
						+ subjectLecturers + " where lecturer_id=" + userId
						+ ";");
				ArrayList<Integer> subjects = new ArrayList<Integer>();
				while (rs.next()) {
					subjects.add(rs.getInt("subject_id"));
				}
				User user = new User(userId, username, name, surname, password,
						status, info, question, answer, pic, subjects, null,
						null);
				return user;
			}
			User user = new User(userId, username, name, surname, password,
					status, info, question, answer, pic, null, null,
					null);
			return user;

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public int getUserId(String name, String surname) {
		try {
			ResultSet rs = statement.executeQuery("select user_id from "
					+ table + " where name='" + name + "' and surname='"
					+ surname + "\';");
			rs.next();
			return rs.getInt("user_id");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	public User[] getUsersByStatus(String status) {
		try {
			ResultSet usr = statement.executeQuery("Select * from " + table
					+ " where status = '" + status + "';");

			usr.last();
			if (usr.getRow() == 0) {
				return null;
			}

			User[] users = new User[usr.getRow()];
			usr.beforeFirst();
			for (int i = 0; usr.next(); i++) {
				int userId = usr.getInt("user_id");
				String username = usr.getString("username");
				String name = usr.getString("name");
				String surname = usr.getString("surname");
				String password = usr.getString("password");
				String info = usr.getString("info");
				String question = usr.getString("question");
				String answer = usr.getString("answer");
				String pic = usr.getString("pic");
				User user = new User(userId, username, name, surname, password,
						status, info, question, answer, pic, null, null, null);
				users[i] = user;
			}

			return users;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;

	}

	public static String getUsername(int user_id) {
		try {
			ResultSet rs = statement.executeQuery("Select username from "
					+ table + " where user_id=" + user_id + ";");
			rs.next();
			return rs.getString("username");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	public static int getID(String username){
		try {
			ResultSet rs = statement.executeQuery("select user_id from "
					+ table + " where username='" + username + "'");
			rs.beforeFirst();
			rs.next();
			return rs.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

	public static void updateSomething(String username, String columnName,
			String value) {
		try {
			statement.executeUpdate("Update " + table + " set " + columnName
					+ "='" + value + "' where username='" + username + "';");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}
